package com.hdax.dm.mapstructs;

import com.hdax.dm.dtos.scheduler.PriceDto;
import com.hdax.dm.entity.scheduler.DmSchedulerSeatPrice;
import com.hdax.dm.service.SchedulerSeatService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper(componentModel = "spring")
public abstract class PriceDtoMapper {


    @Mappings({
            @Mapping(target = "isHaveSeat",expression = "java(calcSeat(source))")
    })
    public abstract PriceDto toTarget(DmSchedulerSeatPrice source);
    public abstract List<PriceDto> toTargetList(List<DmSchedulerSeatPrice> sources);

    @Autowired
    private SchedulerSeatService schedulerSeatService;
    public Short calcSeat(DmSchedulerSeatPrice source){
        int saleCount = schedulerSeatService.getSaleCount(source.getScheduleId(), source.getAreaLevel());
        if(saleCount != source.getSeatNum()){
            return 1;
        }
        return 0;
    }

}
